<?php

namespace App\Process;

use App\Service\ElasticSearch;
use http\Client;
use Hyperf\Contract\StdoutLoggerInterface;
use Hyperf\Logger\LoggerFactory;
use Hyperf\Process\AbstractProcess;
use Hyperf\Redis\Redis;
use Psr\Container\ContainerInterface;


/**
 * 日志消费
 */
class HigoLogConsumer extends AbstractProcess
{

    const LOG_TYPE = [
        'higo_test_api_log',
        'higo_product_api_log',

        'higo_test_log',
        'higo_product_log',

        'fft_test_log',
        'fft_product_log',

        'fft_test_api_log',
        'fft_product_api_log',

        'fyt_test_log',
        'fyt_product_log',

        'fyt_test_api_log',
        'fyt_product_api_log',
    ];

    const LOG_MAP = [
        'higo_product_log' => '嗨购生产环境日志',
        'fft_product_log'  => '福分通生产环境日志',
        'fyt_product_log'  => '福云通生产环境日志',
    ];

    public function handle(): void
    {
        $redis = $this->container->get(Redis::class);
        $es    = new ElasticSearch();

        try {
            $response = $es->es_client->ping([]);
            echo '连接成功';
        } catch (Exception $e) {
            echo '连接失败: ',  $e->getMessage(), "\n";
            exit;
        }
//        $r = $es->indexEs([
//            'index' => 'fyt_product_api_log'.date('_Y_m'),
//            'body'  => ["msg" => "这是一次测试111"],
//            'type'  => "error"
//        ]);
//        var_dump($r);
//        exit;

        $log = $this->container->get(LoggerFactory::class)->get();
        while (true) {
            try {
                foreach (self::LOG_TYPE as $type) {
                    $msgList = $redis->rPop($type);
                    if (!$msgList) {
                        continue;
                    }
                    if (!empty($msgList)) {
                        $msgList = json_decode($msgList, true);
                        if (!empty($msgList)) {
                            //写入es
                            foreach ($msgList as $msg) {
                                $msg = json_decode($msg, true);
                                $es->indexEs([
                                    'index' => $type.date('_Y_m'),
                                    'body'  => $msg,
                                    'type'  => $msg['type']
                                ]);
                                //钉钉提醒
                                // if (in_array($msg['type'], ['error', 'msg_notice']) && in_array($type, ['higo_product_log', 'fft_product_log', 'fyt_product_log'])) {
                                //     $msgStr = json_encode($msg, JSON_UNESCAPED_UNICODE);
                                //     if(strpos($msgStr, 'access_token已过期') !== false){
                                //         $msgStr = "access_token已过期";
                                //     }else{
                                //         (new \GuzzleHttp\Client())->request('POST', 'https://v3.api.fufentong.com/open/ding/sendText', [
                                //             'body'                                => json_encode([
                                //                 'group' => 6,
                                //                 'msg'   => self::LOG_MAP[$type] . ":" . $msgStr
                                //             ], JSON_UNESCAPED_UNICODE), 'headers' => ['content-type' => 'application/json', 'version' => 'mobile']
                                //         ]);
                                //     }
                                // }
                            }
                        }
                    }
                }
                usleep(100);
            } catch (\Throwable $throwable) {
                $log->error($throwable->getMessage());
            }
        }
    }
}